﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleLib.Menu.TextualMenu;
using System.IO;
using frag_boardgame;


namespace ConsoleLib
{
    
    public enum ToShow { MainMenu, Game };
    public class ConsoleUI
    {
        static string lastMessage = null;
        


        public static void SetLastMessage(string text)
        {
            lastMessage=text;
        }

       

      





        public static void WriteToConsole(string p)
        {
            Console.WriteLine(p);
        }
        public static void WriteToConsole(char c)
        {
            Console.Write(c);
        }


        public static void WriteToConsole()
        {
            Console.WriteLine();
        }

        public static void PrintCurentPlayer(GameData data)
        {
            WriteToConsole("Current player is: " + data.CurrentPlayer);
        }

        public static void PrintValues(GameData data)
        {
            Console.Clear();
            PrintBoard(data.Board);
            PrintCurentPlayer(data);
            PrintPos(data);
            PrintMovesLeft(data);
            PrintShotsLeft(data);
            PrintPlayersLife(data);
            PrintLastMessage();
        }

        static void PrintLastMessage()
        {
            if (lastMessage != null)
            {
                ConsoleUI.WriteToConsole(lastMessage);
            }
        }

        public static void PrintShotsLeft(GameData data)
        {
            Player player = data.CurrentPlayer;
            ConsoleUI.WriteToConsole("Shots Left: " + player.ShotsLeft);
        }
        public static void PrintMovesLeft(GameData data)
        {
            ConsoleUI.WriteToConsole("Moves Left: " + data.CurrentPlayer.MovesLeft);
        }

        public static void PrintPos(GameData data)
        {
            Player current = data.CurrentPlayer;
            ConsoleUI.WriteToConsole("Current player is on [" + current.Row + "," + current.Column + "]");
        }
        public static void PrintPlayersLife(GameData data)
        {
            foreach (Player player in data.GetPlayersList())
            {
                WriteToConsole(player.Name + "'s stats: Health=" + player.Health + ", Speed=" + player.Speed + ", Accuracy=" + player.Accuracy);
            }
        }





        public static GameConfiguration GetConfiguration()
        {
            
           
            GameConfiguration conf = new GameConfiguration();
            string read;
            //by default the board size is [16,25].  it will be changed only if the client will input "good" values

            Console.Write("Enter name of board to use [default is empty]:");
            read = Console.ReadLine();
            Console.Write("\n");
            
            if (read != "")
            {
                conf.BoardName = read;
            }


            if (conf.BoardName == null)
            {
                Console.Write("Enter number of rows [default is 16]:");
                read = Console.ReadLine();
                Console.Write("\n");
                int tmp;
                if (int.TryParse(read, out tmp) && tmp > 1)
                {
                    conf.Rows = tmp;
                }
                else
                {
                    if (read != "") Console.WriteLine("Value must be greater than 1! Set for default value");
                    conf.Rows = 16;
                }



                Console.Write("Enter number of columns [default is 25]:");
                read = Console.ReadLine();
                Console.Write("\n");
                if (int.TryParse(read, out tmp) && tmp > 1)
                {
                    conf.Cols = tmp;
                }
                else
                {
                    if (read != "") Console.WriteLine("Value must be greater than 1! Set for default value");
                    conf.Cols = 25;
                }

            }

            //Gets the position:
            Console.WriteLine("How many Players?");
            while (true)
            {
                int tmp;
                read = Console.ReadLine();
                if (int.TryParse(read, out tmp) && tmp > 1 && tmp <= 9)
                {
                    conf.NumberOfPlayers = tmp;
                    break;
                }
                else
                {
                    Console.WriteLine("Value must be numeric between 2 and 9. Try again.");
                }
            }

            return conf;

        }


        


        public static void PrintBoard(Board board)
        {
            char[,] boardLayout = new char[board.Height * 2 + 1, board.Width * 2 + 1];
            
            for (int i = 0; i < board.Height + 1; i++)
            {
                for (int j = 0; j < board.Width  + 1; j++)
                {
                    boardLayout[i *2, j * 2 ] = '+';
                }
            }

            for (int i = 0; i < board.Height; i++)
            {
                for (int j = 0; j < board.Width; j++)
                {
                    if (board.GetSquare(i, j).HasWall(SquareWalls.Down))
                        boardLayout[i * 2 + 0, j * 2 + 1] = '-';
                
                    if (board.GetSquare(i, j).HasWall(SquareWalls.Up))
                        boardLayout[i * 2 + 2, j * 2 + 1] = '-';
                    
                    if (board.GetSquare(i, j).HasWall(SquareWalls.Left))
                        boardLayout[i * 2 + 1, j * 2 + 0] = '|';
                    
                    if (board.GetSquare(i, j).HasWall(SquareWalls.Right))
                        boardLayout[i * 2 + 1, j * 2 + 2] = '|';

                    if (board.GetSquare(i, j).player != null)
                        boardLayout[i * 2 + 1, j * 2 + 1] = board.GetSquare(i, j).player.ID.ToString().ToCharArray()[0];
                }
            }

            for (int i = 0; i < board.Height*2 + 1; i++)
            {
                string cline = "";
                for (int j = 0; j < board.Width*2 + 1; j++)
                {
                    //cline += boardLayout[i,j];
                    char c = boardLayout[i, j];
                    switch (c) 
                    {
                        case '|' :
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            break;
                        case '-':
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            break;
                        case '+':
                            Console.ForegroundColor = ConsoleColor.Gray;
                            break;
                        default:
                            Console.ForegroundColor = ConsoleColor.White;
                            break;
                    }
                    
                    Console.Write(boardLayout[i, j]);
                    Console.ForegroundColor = ConsoleColor.Gray;
                }
                
                //WriteToConsole(cline);
                WriteToConsole();
            }


            
        }



        
    }
}
